package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.MemberReservationDto;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author alexis
 * @date 2025-06-04
 */
@Api(tags = "预约信息管理")
@RestController
@RequestMapping("/member/reservation")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List> countByTime(Long time) {
       List<TimeCountVo> countTime = reservationService.countByTime(time);
        return R.ok(countTime);
    }

    /**
     * 查询当天已取消预约次数
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询当天已取消预约次数")
    public R<Integer> getCancelledReservationCount() {
        //获取当前登录用户id, 根据当前用户去查找预约取消次数
        Long userId = UserThreadLocal.getUserId();
        int count = reservationService.getCancelledCount(userId);
        //data : count
        return R.ok(count);
    }

    /**
     * 分页查询预约信息
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R page(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                                           @RequestParam(value = "status", required = false)Integer status) {

        TableDataInfo tableDataInfo = reservationService.selectPage(pageNum, pageSize, status);
        return R.ok(tableDataInfo);

    }

    /**
     * 新增预约
     * @param memberReservationDto
     * @return
     */
    @PostMapping
    @ApiOperation("新增预约")
    public R add(@ApiParam("预约信息") @RequestBody MemberReservationDto memberReservationDto) {
        int reservation = reservationService.add(memberReservationDto);
        return R.ok(reservation);
    }


    /**
     *  取消预约
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id) {

        return toAjax(reservationService.cancelReservation(id));
    }



}
